package com.itheima.test5case2;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;


public class Test {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建接口的实现类对象
        MyCallable mc=new MyCallable();
        //创建FutureTask对象
        FutureTask<ArrayList<Integer>> ft1=new FutureTask<>(mc);
        FutureTask<ArrayList<Integer>> ft2=new FutureTask<>(mc);
        //创建两个线程，两个抽奖箱
        Thread t1=new Thread(ft1);
        Thread t2=new Thread(ft2);
        //设置名字
        t1.setName("抽奖箱1");
        t2.setName("抽奖箱2");
        //启动线程
        t1.start();
        t2.start();
        //获取多线程运行的结果
        ArrayList<Integer> list1 = ft1.get();
        ArrayList<Integer> list2 = ft2.get();

       /* System.out.println(list1);
        System.out.println(list2);*/

        sout(t1, list1);
        sout(t2, list2);

        int max=getMax(list1)>getMax(list2)?getMax(list1):getMax(list2);
        if(max==getMax(list1)){
            System.out.println("在此次抽奖过程中，"+t1.getName()+"中产生了最大奖项，该项金额为"+max+"元");
        }else {
            System.out.println("在此次抽奖过程中，"+t2.getName()+"中产生了最大奖项，该项金额为"+max+"元");
        }




    }

    private static void sout(Thread t, ArrayList<Integer> list) {
        System.out.println("在此次抽奖过程中，"+ t.getName()+
                "总共产生了"+ list.size()+"个奖项，" +
                "分别为："+ list +"最高奖项为"+getMax(list)+"元，总计金额为"+getSum(list)+"元");
    }

    //计算每个抽奖箱的最大奖
    private static int getMax(ArrayList<Integer> list1) {
        int max = list1.get(0);
        for (int i = 1; i < list1.size(); i++) {
            if (list1.get(i) > max) {
                max = list1.get(i);
            }
        }
        return max;
    }

    //统计每个抽奖箱的总金额
    private static int getSum(ArrayList<Integer> list1) {
        int sum = 0;
        for (int i = 0; i < list1.size(); i++) {
            sum += list1.get(i);
        }
        return sum;
    }
}
